package lgkj.meicqt20.ui;

import android.content.Intent;
import android.util.Log;
import android.widget.GridView;
import android.widget.ImageView;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import lgkj.meicqt20.R;
import lgkj.meicqt20.adapter.CollegeNewAdapter;
import lgkj.meicqt20.bean.MessageEvent;
import lgkj.meicqt20.bean.TrainEnitity;
import lgkj.meicqt20.utils.AllKindsOfConstant;
import lgkj.meicqt20.utils.HttpUtils;
import lgkj.meicqt20.view.BannerViewpager;

/**
 * Created by Administrator on 2019/1/18 0018.
 * 美点学院
 */

public class CollegeActivity extends BaseActivity {
    @BindView(R.id.mdxy)
    ImageView mdxy;
    @BindView(R.id.jin_xi)
    ImageView jinXi;
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.vp1)
    BannerViewpager vp1;
    @BindView(R.id.gv_item)
    GridView gvItem;
    @BindView(R.id.ibtn_training)
    ImageView ibtnTraining;
    @BindView(R.id.ibtn_video)
    ImageView ibtnVideo;
    @BindView(R.id.ibtn_fashion)
    ImageView ibtnFashion;


    private ImageView[] headViews = new ImageView[3];
    private int headPosition;  //头部
    private CollegeNewAdapter adapter;
    private TrainEnitity trainEnitity1;   //培训
    private TrainEnitity trainEnitity2;   //前沿
    private int adapterPositon;
    private TrainEnitity trainEnitity3;   //教程
    private boolean isTop = true;
    private int oldId;

    @Override
    public int setContentView() {
        return R.layout.activity_college;
    }

    @Override
    public void init() {
        activitiesList.add(this);
        displayDifferen();
        bandViewInit(new initBannersDataResult() {
            @Override
            public void resultSuccess(List<String> list) {
                vp1.setData(list);
            }
        });
        HttpUtils.getQRCode(CollegeActivity.this, ivCode);
        adapter = new CollegeNewAdapter(CollegeActivity.this);
        headViews[0] = ibtnTraining;
        headViews[1] = ibtnVideo;
        headViews[2] = ibtnFashion;
        getfashion();
        getSchool();
        getvidoes();
        gvItem.setAdapter(adapter);
        oldId = -2;
        headPosition = 0;
        adapterPositon = -1;
        isTop = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
        Log.d(TAG, "onResume");


    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        Log.i(TAG, "socket: " + messageEvent.toString());
        if ("control".equals(messageEvent.getTagName())) {
            if (isTop) {
                fragmentDealOrder(messageEvent.getMessage());
            } else {
                itemOrder(messageEvent.getMessage());
            }
            if (oldId != adapterPositon) {
                oldId = adapterPositon;
                adapter.setPosition(adapterPositon);
                gvItem.smoothScrollToPositionFromTop(adapterPositon, 0);
                adapter.notifyDataSetChanged();
            }
        }
    }

    private void headEnabled(int mpositon) {
        for (int i = 0; i < headViews.length; i++) {
            if (i == mpositon) {
                headViews[i].setEnabled(true);
            } else {
                headViews[i].setEnabled(false);
            }
        }
        if (mpositon == 0) {
            adapter.setData(trainEnitity1);
        } else if (mpositon == 1) {
            adapter.setData(trainEnitity3);
        } else {
            adapter.setData(trainEnitity2);
        }
        Log.i(TAG, "count:" + adapter.getCount() + ",mpositon:" + mpositon);
    }

    /**
     * top操作  true
     *
     * @param order
     */
    private void fragmentDealOrder(String order) {
        switch (order) {
            case "down":
                isTop = false;
                adapterPositon = 0;
                break;
            case "left":
                if (headPosition > 0) {
                    headPosition--;
                } else {
                    headPosition = 0;
                }
                headEnabled(headPosition);
                adapterPositon = -1;
                break;
            case "right":
                if (headPosition < 2) {
                    headPosition++;
                } else {
                    headPosition = 2;
                }
                adapterPositon = -1;
                headEnabled(headPosition);
                break;
            case "back":
                finish();
                break;

        }
//        if (oldId != adapterPositon) {
        oldId = adapterPositon;
        adapter.setPosition(adapterPositon);
        gvItem.smoothScrollToPositionFromTop(adapterPositon, 0, 800);
        gvItem.postDelayed(new Runnable() {
            @Override
            public void run() {
                gvItem.setSelection(adapterPositon);
            }
        }, 900);
        adapter.notifyDataSetChanged();
//        }
    }

    private void itemOrder(String str) {
        switch (str) {
            case "up":
                if (adapterPositon == 0) {
                    adapterPositon = -1;
                    isTop = true;
                } else {
                    if (adapter.getCount() < 4) {
                        adapterPositon = 0;
                    } else {
                        adapterPositon = adapterPositon - 4;
                        if (adapterPositon <= -1) {
                            adapterPositon = -1;
                            isTop = true;
                        }
                    }
                }
                break;
            case "down":
                if (adapter.getCount() < 4) {
                    adapterPositon = 0;
                } else {
                    adapterPositon = adapterPositon + 4;
                    if (adapter.getCount() <= adapterPositon) {
                        adapterPositon = adapter.getCount() - 1;
                    }
                }
                break;
            case "left":
                if (adapterPositon > 0) {
                    adapterPositon = adapterPositon - 1;
                }
                break;
            case "right":
                if ((adapterPositon + 1) < adapter.getCount()) {
                    adapterPositon = adapterPositon + 1;
                }
                break;
            case "back":
                finish();
                break;
            case "sure":
                Intent intent = new Intent(CollegeActivity.this, LoadUrlActivity.class);
                try {
                    String vidoesUrl = adapter.getData(adapterPositon).getUrl();
                    Log.i(TAG, "itemOrder: " + vidoesUrl);
                    intent.putExtra("mMovieUrl", vidoesUrl);
                    intent.putExtra("video_name", adapter.getData(adapterPositon).getTitle());
                    startActivity(intent);
                } catch (Exception e) {
                    Log.e(TAG, "itemOrder: " + e.getMessage());
                }
                break;
        }

        Log.i(TAG, "itemOrder====adapterPositon: " + adapterPositon + ",isTop:" + isTop);

    }

    @Override
    public void initData() {

    }


    /**
     * 培训
     */
    private void getSchool() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "1");
        HttpUtils.getHttp(map, AllKindsOfConstant.getFashionList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("status") == 1) {
                    trainEnitity1 = new Gson().fromJson(jsonObject.toString(), TrainEnitity.class);
                }
                adapter.setData(trainEnitity1);
                headEnabled(0);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    /**
     * 教程视频
     */
    private void getvidoes() {
        Map<String, String> getvidoesMap = new HashMap<>();
        getvidoesMap.put("type", "3");
        HttpUtils.getHttp(getvidoesMap, AllKindsOfConstant.getFashionList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("status") == 1) {
                    trainEnitity3 = new Gson().fromJson(jsonObject.toString(), TrainEnitity.class);
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    /**
     * 科技前沿
     */
    private void getfashion() {
        Map<String, String> getfashion = new HashMap<>();
        getfashion.put("type", "2");
        HttpUtils.getHttp(getfashion, AllKindsOfConstant.getFashionList, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("status") == 1) {
                    trainEnitity2 = new Gson().fromJson(jsonObject.toString(), TrainEnitity.class);
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

}
